/* Standard headers */

#include <string>
#include <iostream>
#include <cstdlib>

/* Local headers */
#include "ablibs.h"
#include "abimport.h"
#include "proto.h"

const short MAX_PLANE_IN_RANGE = 50;
const short AIRPLANE_RADIUS = 10;
const PgColor_t RADAR_COLOR = Pg_GREEN;
const PgColor_t AIRPLANE_COLOR = Pg_YELLOW;
const PgColor_t CROSSHAIR_COLOR = Pg_RED;
const PgColor_t RADAR_TEXT_COLOR = Pg_BLACK; 
const PgColor_t RADAR_STROKE_COLOR = Pg_BLACK;
const int RADAR_SIZE = 700;

// super global variables :P
extern aircraftList al;
extern char AIRPORT_NAME[20];
extern int X_POSITION;
extern int Y_POSITION;


using namespace std;
 
/*
typedef struct
{
	int type;
	int noOfAircarfts;
	aircraft a[MAX_PLANE_IN_RANGE];
} aircraftList;
*/


void Radar_Draw( PtWidget_t *widget, PhTile_t *damage ) 
{

	aircraft planes[50];
 	char Helvetica12[MAX_FONT_TAG];
 	int i,how_many;
 	
 	
	PtSuperClassDraw( PtBasic, widget, damage );
	
	if(PfGenerateFontName("Helvetica", 0, 12, Helvetica12) == NULL) 
     	perror("Unable to find font");
     else 
		PgSetFont( Helvetica12 );


	// stworzenie testowej listy samolotow
	for(i=0; i<10; i++)
	{
		planes[i].Xposition = i*60+30;
		planes[i].Yposition = i*60+30;
		strcpy(planes[i].aircraftName,"567");
		//planes[i].aircraftName = "567";
		planes[i].state = (i%5*3+1);
		planes[i].FuelValue = i*40+10;
	}
	how_many = 10;
	
	// rysujemy radar
	Draw_On_Radar(planes, how_many);
	Samoloty_Info(planes, how_many);
	
}


void Draw_On_Radar(aircraft *planes, int how_many)
{
	PhPoint_t radius = { RADAR_SIZE/2 - AIRPLANE_RADIUS/2 ,RADAR_SIZE/2 - AIRPLANE_RADIUS/2 };
	PhPoint_t center = { RADAR_SIZE/2, RADAR_SIZE/2};
	int i;

	// narysowanie tarczy radaru oraz krzyzyka na srodku
	PgSetStrokeColor(RADAR_STROKE_COLOR);
	PgSetFillColor( RADAR_COLOR );
	PgDrawEllipse( &center, &radius, Pg_DRAW_FILL_STROKE);
	PgSetStrokeColor( CROSSHAIR_COLOR);
	PgDrawILine( center.x - 8, center.y, center.x + 8, center.y );
	PgDrawILine( center.x, center.y - 8, center.x, center.y + 8 ); 

	// narysowanie samolotow i podpisanie ich
	radius.x = AIRPLANE_RADIUS;
	radius.y = AIRPLANE_RADIUS;	
	PgSetFillColor(AIRPLANE_COLOR);
	PgSetTextColor(RADAR_TEXT_COLOR);	
	PgSetStrokeColor(RADAR_STROKE_COLOR);
	
	/*
	for ( i=0 ; i < how_many ; i++)
	{
		center.x = planes[i].Xposition;
		center.y = planes[i].Yposition;
		PgDrawEllipse( &center, &radius, Pg_DRAW_STROKE_FILL);	
		PgDrawText( planes[i].aircraftName,strlen(planes[i].aircraftName), &center, Pg_TEXT_CENTER);
	}
	*/

	// updated by Vizaard
	for ( i=0 ; i < al.noOfAircrafts ; i++)
	{
		center.x = al.a[i].Xposition;
		center.y = al.a[i].Yposition;
		PgDrawEllipse( &center, &radius, Pg_DRAW_STROKE_FILL);	
		PgDrawText( al.a[i].aircraftName,strlen(planes[i].aircraftName), &center, Pg_TEXT_CENTER);
	}
	// end of updation	
	
}



int Samoloty_Info(aircraft *planes, int how_many)
{

	PtWidget_t *pan, *parent, *wid;
	PhArea_t area = { {100,100}, {500,500}}; 
	PhPoint_t c = {100,100};
	PhPoint_t c2 = {650,42};
	PgColor_t color = Pg_BLUE;
	PtArg_t argt[10];
	char text[20];
	int i;
	/* przyklad
	PtSetArg (&argt[0], Pt_ARG_TEXT_STRING, "Napis", 0);
	
	PtSetArg (&argt[1], Pt_ARG_POS,  &c, 0);
 	
	parent = ApGetWidgetPtr(ABW_Samoloty_PtPane,  ABN_Samoloty_PtPane );
	
	PtRealizeWidget( wid);*/

	parent = ApGetWidgetPtr(ABW_Samoloty_PtScrollContainer ,  ABN_Samoloty_PtScrollContainer  );
	
	for( i=0; i<how_many; i++)
	{
		// stworzenie panelu dla pozostalch widgetow
		c.x = 10;
		c.y = i*57 +10;
		PtSetArg(&argt[0], Pt_ARG_POS, &c, 0);
		PtSetArg(&argt[2], Pt_ARG_FILL_COLOR, &color, 0);
		PtSetArg(&argt[1], Pt_ARG_DIM, &c2, 0);
	
		pan = PtCreateWidget(PtPane, parent, 3, argt);
		PtRealizeWidget(pan);
	
		// stworzenie labelu z nazwa samolotu
		c.x = 20;
		c.y = 12;
		PtSetArg (&argt[0], Pt_ARG_POS,  &c, 0);
		sprintf(text,"Lot: %s",planes[i].aircraftName);		
		PtSetArg (&argt[1], Pt_ARG_TEXT_STRING, text, 0);
		wid = PtCreateWidget(PtLabel, pan, 2, argt);		
		PtRealizeWidget(wid);		
		
		// stworzenie labelu ze stan samolotu
		c.x = 160;
		sprintf(text,"Stan: %d",planes[i].state);
		PtSetArg (&argt[0], Pt_ARG_POS,  &c, 0);		
		PtSetArg (&argt[1], Pt_ARG_TEXT_STRING, text , 0);
		wid = PtCreateWidget(PtLabel, pan, 2, argt);		
		PtRealizeWidget(wid);
		
		// stworzenie labelu z iloscia paliwa
		c.x = 300;
		sprintf(text,"Paliwo: %d",planes[i].FuelValue);
		PtSetArg (&argt[0], Pt_ARG_POS,  &c, 0);		
		PtSetArg (&argt[1], Pt_ARG_TEXT_STRING, text , 0);
		wid = PtCreateWidget(PtLabel, pan, 2, argt);		
		PtRealizeWidget(wid);
		// stworzenie labelu z iloscia paliwa
		
		c.y = 6;
		c.x = 440;
		PtSetArg (&argt[0], Pt_ARG_POS,  &c, 0);		
		PtSetArg (&argt[1], Pt_ARG_TEXT_STRING, "Wyslij wiadomosc" , 0);
		wid = PtCreateWidget(PtButton, pan, 2, argt);		
		PtRealizeWidget(wid);
		
	}
}
/*
	int c,errflag=0;
	int fd;
	packet_t msg;
	packet_t reply;
	
	if(argc<2)
	{
	    printf("To less arguments.\nUsage: ./a airport_name\n");
	    exit(1);
	}
	
	printf("%s %s\n",argv[0],argv[1]);
	msg.who=10;
	msg.type=2;
	printf("SDSDSDSD %d ___ %d\n", msg.who,msg.type);
	
	if((fd = name_open(argv[1],NAME_FLAG_ATTACH_GLOBAL)) == -1)
	{
	    printf("Cannot get to %s\n",argv[1]);
	    exit(1);
	}
	
	while(1)
	{
	    msg.who=10;
	    msg.type=2;
	    MsgSend(fd,&msg,sizeof(msg),&reply,sizeof(reply));
	    if(reply.who==666)
	    {
		printf("I've received data:\n");
		printf("Airport Name: %s\n",reply.lotnisko.airportName);
		printf("X position on a map: %d\n",reply.lotnisko.X);
		printf("Y position on a map: %d\n",reply.lotnisko.Y);
		printf("Range radius: %d\n",reply.lotnisko.Range);
		printf("Capacity: %d\n",reply.lotnisko.Capacity);
		printf("Runways Number: %d\n",reply.lotnisko.RunwaysNumber);
		printf("State: %d\n",reply.lotnisko.State);		
		break;
	    }
	    else
	    {
		printf("Fucki'n hell\n");
	    }
	    sleep(1);
	}
	
	printf("MAM DANE\n");
*/


